<?php
/**
 * Pert PHP Library
 * Model Abstract Classes
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pert;

abstract class model
{
	private $id;

	protected function __construct($id)
	{
		$this->id = $id;
	}

	public function Id()
	{
		return $this->id;
	}

	public function __ToString()
	{
		return $this->id;
	}
}

abstract class reSelectableModel extends model
{
	private static $arrayArray;

	protected function __construct($id)
	{
		parent::__construct($id);

		self::$arrayArray[get_called_class()][$this->Id()] = $this;
	}

	final protected static function get($id)
	{
		if(isset(self::$arrayArray[get_called_class()][$id]))
			return self::$arrayArray[get_called_class()][$id];
	}

	final protected static function getArray(Array $idArray)
	{
		$array = array();

		foreach($idArray AS $id)
			if(isset(self::$arrayArray[get_called_class()][$id]))
				$array[] = self::$arrayArray[get_called_class()][$id];

		return $array;
	}

	final protected static function hasArray()
	{
		return isset(self::$arrayArray[get_called_class()]);
	}

	final protected static function getAllArray()
	{
		if(isset(self::$arrayArray[get_called_class()]))
			return self::$arrayArray[get_called_class()];

		return array();
	}
}

interface Object
{
	public function Id();

	public function __ToString();
}

interface Selectable extends Object
{
	public static function Select($id);
}

interface ArraySelectable extends Object
{
	public static function SelectArray(Array $idArray);
}

interface AllArraySelectable extends Object
{
	public static function SelectAllArray();
}

abstract class onlyAllSelectableModel extends reSelectableModel implements Selectable,
		ArraySelectable,
		AllArraySelectable
{
	final public static function Select($id)
	{
		if(!self::hasArray())
			static::SelectAllArray();

		return self::get($id);
	}

	final public static function SelectArray(Array $idArray)
	{
		if(!self::hasArray())
			static::SelectAllArray();

		return self::getArray($idArray);
	}
}
